/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.notaria.guerrasalas.be;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

/**
 *
 * @author Administrador
 */
@Entity
@Table(name = "COMPRA_VENTA_VEHICULO", catalog = "BASE_NOTARIAL", schema = "dbo")
@PrimaryKeyJoinColumn(name = "ID_COMPRA_VENTA_VEHICULO", referencedColumnName = "ID_ACTO_JURIDICO")
@DiscriminatorValue(value = "CVV")
@NamedQueries({
    @NamedQuery(name = "CompraVentaVehiculo.findAll", query = "SELECT c FROM CompraVentaVehiculo c"),
    @NamedQuery(name = "CompraVentaVehiculo.findByKardex", query = "SELECT c FROM CompraVentaVehiculo c WHERE c.kardex.idKardex = ?1 AND c.estado = ?2"),
    @NamedQuery(name = "CompraVentaVehiculo.findByVehiculo", query = "SELECT c FROM CompraVentaVehiculo c WHERE UPPER(c.vehiculo.nroPlaca) LIKE ?1 OR UPPER(c.vehiculo.dua) LIKE ?2 ORDER BY c.fechaCreacion DESC"),
    @NamedQuery(name = "CompraVentaVehiculo.findByParticipante", query = "SELECT c FROM CompraVentaVehiculo c INNER JOIN c.detalleParticipantes p WHERE UPPER(p.cliente.nombres) Like :nombres or UPPER(p.cliente.apellidoPaterno) Like :apPaterno or UPPER(p.cliente.apellidoMaterno) Like :apMaterno ORDER BY c.fechaCreacion DESC"),
    @NamedQuery(name = "CompraVentaVehiculo.findBetweenDates", query = "SELECT c FROM CompraVentaVehiculo c WHERE c.fechaCreacion BETWEEN :fechaInicio AND :fechaFin ORDER BY c.fechaCreacion DESC")
})
public class CompraVentaVehiculo
        extends ActoJuridicoConParticipantes
        implements Serializable {

    @Enumerated(EnumType.ORDINAL)
    @Basic(optional = false)
    @Column(name = "TIPO", nullable = false)
    private TipoCompraVentaVehiculo tipo = TipoCompraVentaVehiculo.CompraVenta;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "compraVentaVehiculo", fetch = FetchType.LAZY)
    private ActaVehicular actaVehicular;
    @JoinColumn(name = "FID_VEHICULO", referencedColumnName = "ID_VEHICULO", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Vehiculo vehiculo;

    public CompraVentaVehiculo() {
    }

    public CompraVentaVehiculo(Long idActoJuridico) {
        super(idActoJuridico);
    }

    public ActaVehicular getActaVehicular() {
        return actaVehicular;
    }

    public void setActaVehicular(ActaVehicular actaVehicular) {
        this.actaVehicular = actaVehicular;
    }

    public TipoCompraVentaVehiculo getTipo() {
        return tipo;
    }

    public void setTipo(TipoCompraVentaVehiculo tipo) {
        this.tipo = tipo;
    }

    public Vehiculo getVehiculo() {
        return vehiculo;
    }

    public void setVehiculo(Vehiculo vehiculo) {
        this.vehiculo = vehiculo;
    }

    public String getComprador() {
        String comprador = "";
        try {
            if (!isDetalleParticipantesEmpty()) {
                for (DetalleClienteActoJuridico item : getDetalleParticipantes()) {
                    if (item.getParticipacion().equals(Participacion.Comprador)) {
                        comprador = (comprador.trim().equals("") ? "" : ",") + item.getCliente().getNombreCompleto();
                    }
                }
            } else {
                comprador = "No se ha encontrado un comprador";
            }
        } catch (Exception e) {
            comprador = e.getMessage();
        }
        return comprador;
    }

    public String getVendedor() {
        String vendedor = "";
        try {
            if (!isDetalleParticipantesEmpty()) {
                for (DetalleClienteActoJuridico item : getDetalleParticipantes()) {
                    if (item.getParticipacion().equals(Participacion.Vendedor)) {
                        vendedor = (vendedor.trim().equals("") ? "" : ",") + item.getCliente().getNombreCompleto();
                    }
                }
            } else {
                vendedor = "No se ha encontrado un vendedor";
            }
        } catch (Exception e) {
            vendedor = e.getMessage();
        }
        return vendedor;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ActoJuridico other = (ActoJuridico) obj;
        if (this.idActoJuridico != other.idActoJuridico && (this.idActoJuridico == null || !this.idActoJuridico.equals(other.idActoJuridico))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 61 * hash + (this.idActoJuridico != null ? this.idActoJuridico.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return idActoJuridico.toString();
    }
}
